﻿using IoTGateway.Model;
using Microsoft.EntityFrameworkCore;
using OpenIotMain.SQLiteTool;
using OpenIotModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Entity;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace OpenIotMain {
    public partial class pointAddForm : Form {
        public string DeviceId { get; set; }
        SqliteDbContext dbcontext = new SqliteDbContext();
        public pointAddForm(string deviceId) {
            InitializeComponent();
            DeviceId = deviceId;
            // 创建一个 DataGridViewCheckBoxColumn
            DataGridViewCheckBoxColumn checkBoxColumn = new DataGridViewCheckBoxColumn();
            checkBoxColumn.HeaderText = "选择"; // 列的标题
            checkBoxColumn.Name = "checkBoxColumn"; // 列的名称，可以用于引用这个列

            // 将列添加到 dataGridView1 中
            dataGridView1.Columns.Add(checkBoxColumn);

            // 设置列的位置（可选）
            checkBoxColumn.DisplayIndex = 0; // 这里假设你希望选择框列显示在第一列

            // 允许用户选择多行（可选）
            dataGridView1.MultiSelect = true;


        }

        private void button1_Click(object sender, EventArgs e) {
            DeviceVariable deviceVariable = new DeviceVariable();
            deviceVariable.ID = Guid.NewGuid().ToString();
            deviceVariable.Name = textBox1.Text;
            deviceVariable.Description = textBox2.Text;
            deviceVariable.DeviceAddress = textBox3.Text;
            deviceVariable.DataType = comboBox1.Text;
            deviceVariable.Index = 0;
            deviceVariable.Method = "Read";
            deviceVariable.IsUpload = false;
            deviceVariable.DeviceId = DeviceId;
            dbcontext.deviceVariable.Add(deviceVariable);
            dbcontext.SaveChanges();
            MessageBox.Show("添加成功");
            var list = dbcontext.deviceVariable.Where(x => x.DeviceId == DeviceId).ToList();
            dataGridView1.DataSource = list;

        }

        private void pointAddForm_Load(object sender, EventArgs e) {
            var list = dbcontext.deviceVariable.Where(x => x.DeviceId == DeviceId).ToList();
            dataGridView1.DataSource = list;
            dataGridView1.Columns["ID"].Visible = false;
            dataGridView1.Columns["Index"].Visible = false;
            dataGridView1.Columns["IsUpload"].Visible = false;
            dataGridView1.Columns["DeviceId"].Visible = false;
        }

        private void button2_Click(object sender, EventArgs e) {
            foreach (DataGridViewRow row in dataGridView1.Rows) {
                DataGridViewCheckBoxCell checkBoxCell = (DataGridViewCheckBoxCell)row.Cells[0];
                if (checkBoxCell.Value != null && (bool)checkBoxCell.Value) {
                    //selectedRows.Add(row);
                    string id = row.Cells[1].Value.ToString();
                    DeviceVariable deviceVariable = dbcontext.deviceVariable.Find(id);
                    dbcontext.deviceVariable.Remove(deviceVariable);
                }
            }
            dbcontext.SaveChanges();
            MessageBox.Show("删除成功");
            var list = dbcontext.deviceVariable.Where(x => x.DeviceId == DeviceId).ToList();
            dataGridView1.DataSource = list;
        }
    }
}
